//
//  HMWelcomeViewController.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/14.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
         prepareUI()
         setIconView()
       
//        发送网络请求
        HMUserAccountViewModel.sharedInstance.loadUserInfo { (error) in
            if error != nil {
            HMPrint("获取用户信息错误:\(error)")
            return
            }
//            获取用户信息成功
            self.setIconView()
        }
        
        
    }
    
    private func setIconView(){
    
//    设置头像
        if let avatar_large = HMUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url  = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage.init(named: "avatar_default_big"))
            
        }
    
    
    }
    
//   加载子控件
    private func prepareUI(){
//    添加到控制器view上
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
//    添加约束
//        取消自动布局
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        bgImageView.snp_makeConstraints { (make) in
           make.edges.equalTo(UIEdgeInsetsZero)
        }
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
        }
        welcomeLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    
    }
//    在view出现的时候开始动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewAnimation()
        
    }
    
//   设置动画实现方法
    private func iconViewAnimation(){
    
//    更新约束update 这个方法不会立即执行更新frame，
        iconView.snp_updateConstraints { (make) in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height-160))
        }
    UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: {
        
//        通知系统更新约束
        self.view.layoutIfNeeded()
        
        }) { (_) in
            UIView.animateWithDuration(HMDefaultAnimationDur, animations: { 
                self.welcomeLabel.alpha = 1
                },completion: { (_) -> Void in
            

                    let controller = HMMainViewController()
                    (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
            
            })
            
            
        }
    
    
    }
    
    
    
//MARK: 懒加载
//    背景图片
    private lazy var bgImageView : UIImageView = UIImageView(image: UIImage.init(named: "ad_background"))
//    头像
    private lazy var iconView : UIImageView = {
    
        let imageView = UIImageView(image: UIImage.init(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
//     裁剪
        imageView.layer.masksToBounds = true
    
        return imageView
    }()
    
//     欢迎文字
    private lazy var welcomeLabel : UILabel = {
    
        let label = UILabel()
        label.text = "欢迎"
        label.textColor = UIColor.blackColor()
        label.alpha = 0
        label.font = UIFont.systemFontOfSize(HMWelcomeLabelFontSize)
    
        return label
    }()
    
    
    
   
    
}
